package org.openzip.openzip.archiving.gui;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

/**
 * This is a "hack" class needed to create a (hidden) root node for the
 * Checkboxed tree in the archiving logic. Because <code>FileTreeNode</code>
 * demands a file in the constructor, this is a "wrapper" <code>File</code>
 * class for representing the root "file" of all local filesystem roots (C:, D:,
 * E:, etc...).
 * 
 * @author Jakub Závěrka
 */
public class TreeRootFile extends File
{
	/**
	 * Creates a new <code>TreeRootFile</code>. File constructor demands that the
	 * specified path must exists, therefore we need a file that will always
	 * be present, regardless the platform. It uses the user's home directory. 
	 */
	public TreeRootFile(){
		//User's home directory is chosen.
		super(System.getProperty("user.dir"));
		
		//Debug
		System.out.println("Calling super(\"user.dir\")");
		System.out.println("Dir: " + this.getPath());
		File[] roots = File.listRoots();
		String stringRoots = "";
		for (File root:roots) {
			stringRoots += root.getPath();
		}
		System.out.println("Constructor File roots: " + stringRoots);
		String stringChildren = "";
		String[] children = this.list();
		for (String child:children) {
			stringChildren += " " + child;
		}
		System.out.println("Constructor Children: " + stringChildren);
	}
	
	/**
	 * Returns the name of the file. In this case, "root". 
	 */
	public String getName(){
		return "root";
	}
	
	/**
	 * This method lists all the children files for this file. This 
	 * implementation returns all filesystem roots on the current platform and
	 * therefore ensures instance of this class behave like a parent of all
	 * filesystem roots despite the folder "user.dir" from which it has been 
	 * created.
	 */
	public File[] listFiles(){
		System.out.println("listFiles() has been called");
		File[] roots = File.listRoots();
		String stringRoots = "";
		for (File root:roots) {
			stringRoots += root.getPath();
		}
		System.out.println("listFiles() File roots: " + stringRoots);
		return File.listRoots();
	}
	
	public String[] list(FilenameFilter filter) {
		File[] roots = File.listRoots();
		List<String> rootsList = new ArrayList<String>();
		for (File root:roots) {
			rootsList.add(root.getPath());
		}
		String[] rootsArray = {""};
		return rootsList.toArray(rootsArray);
	}
}
